﻿<%@ WebHandler Language="C#" Class="empvalidate" %>

using System;
using System.Web;
using System.Data;
using NS.Utilities;
using BLL;
using Model;

public class empvalidate : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string result = string.Empty;
        if (!string.IsNullOrEmpty(CookieHelper.GetValue("openid")))
        {
            if (UserService.IsActivate(CookieHelper.GetValue("openid")))
            {
                result = JsonMessager.Success("ok");
            }
            else
            {
                result = JsonMessager.Success("empactive.html");
            }
        }
        else
        {
            if (string.IsNullOrEmpty(context.Request["code"]))
            {
                string url = "http://" + context.Request.Url.Host + "/wechat/empvalidate.html";
                result = JsonMessager.Success(Wechat.GetOAuthBasePublic(url));
            }
            else
            {
                //获取用户的openid
                Wechat wc = new Wechat();
                string openid = wc.GetOpenid(context.Request["code"]);
                //string openid = "oNqrqjrO9z53O-0lCFb60QGufjaw";
                if (!string.IsNullOrEmpty(openid))
                {
                    CookieHelper.SetObject("openid", 86400, openid);
                    if (UserService.IsActivate(CookieHelper.GetValue("openid")))
                    {
                        result = JsonMessager.Success("ok");
                    }
                    else
                    {
                        result = JsonMessager.Success("empactive.html");
                    }
                }
                else
                {
                    result = JsonMessager.Fail("用户验证失败");
                }
            }
        }
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}